﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StateMachineCompiler
{
	public class StateMachine
	{
		private State start;
		private List<Event> resetEvents = new List<Event>();

		public StateMachine(State start)
		{
			this.start = start;
		}

		public State Start { get { return start; } }

		public IEnumerable<State> States
		{
			get
			{
				var states = new List<State>();
				CollectStates(states, start);
				return states;
			}
		}

		private void CollectStates(List<State> states, State s)
		{
			if (states.Contains(s))
				return;

			states.Add(s);
			foreach (var next in s.Transitions.Select(t => t.Target))
				CollectStates(states, s);
		}

		public void AddResetEvents(params Event[] events)
		{
			this.resetEvents.AddRange(events);
		}

		public bool IsResetEvent(string eventCode)
		{
			return resetEvents
				.Select(e => e.Code)
				.Where(code => code == eventCode)
				.Any();
		}
	}
}
